package com.wmyg.scanninggun.service;

import com.wmyg.scanninggun.bean.Bill;
import com.wmyg.scanninggun.dao.BillMapper;
import com.wmyg.scanninggun.dao.FabricDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TimingFabricService {

    @Autowired
    BillMapper billMapper;

    @Autowired
    FabricDao fabricDao;

    @Scheduled(cron = "0 0 0 * * ?")
    public void billsUpload() {
        List<Bill> bills = billMapper.selectNotUploadedBills();
        for (Bill bill:
             bills) {
            boolean flag = true;
            if (bill.getState() == 3) {
                try {
                    fabricDao.uploadBill(bill);
                } catch (Throwable throwable) {
                    flag = false;
                }
            }
            if (flag) {
                billMapper.uploaded(bill);
            }
        }
    }

}
